package com.jy.admin.rest;

import com.jsoniter.output.JsonStream;
import com.jy.admin.db.dao.DtQueryLogDao;
import com.jy.admin.db.entity.BillHistoryVo;
import com.jy.admin.db.entity.DtQueryLogEntity;
import com.jy.admin.rest.Response.QueryResponseData;
import com.jy.admin.rest.request.QueryRequest;
import com.jy.bo.common.conf.api.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(("/customer"))
public class VerifyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(VerifyController.class);
    @Autowired
    private DtQueryLogDao queryLogDao;


    @RequestMapping("/orders/query")
    public ApiResponse query(@RequestBody QueryRequest request) {

//        try {
//            LOGGER.info("== VerifyController, query ==,request:{}", JsonStream.serialize(request));
//        } catch (Exception e) {
//            LOGGER.error("== VerifyController, query error ==",e);
//        }
        if (StringUtils.isEmpty(request.getMerchantCode()) || !"8afc568efd213fd".equals(request.getMerchantCode())) {
            return new ApiResponse().setCode(1002).setMessage("invalid merchant");
        }

        queryLogDao.insert(new DtQueryLogEntity().setMerchantCode(request.getMerchantCode()).setQueryType(request.getData().getQueryType()).setQueryValue(request.getData().getQueryValue()));
        List<BillHistoryVo> allBorrowRecordsByMobile = queryLogDao.getAllBorrowRecordsByMobile(request.getData().getQueryValue());

        return new ApiResponse(new QueryResponseData(allBorrowRecordsByMobile));
    }
}
